package com.yang.travel.controller;

import com.yang.travel.mapper.UserMapper;
import com.yang.travel.pojo.UserInfo;
import com.yang.travel.service.UserInfoService;
import com.yang.travel.service.impl.MailService;
import com.yang.travel.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import java.util.Collection;

@Controller
@RequestMapping("/Y_travel/profile")
public class ProfileController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private MailService mailService;
    @GetMapping("/findProfile")
    public String findProfile(HttpSession session, Model model) throws Exception {
        SecurityContextImpl securityContext = (SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT");
        Collection<? extends GrantedAuthority> authorities = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getAuthorities();
        String username = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getUsername();
        UserInfo user = userMapper.findByUsername(username);
        UserInfo userInfo = userInfoService.findById(user.getId());
        model.addAttribute("email",userInfo.getEmail());
        model.addAttribute("phone",userInfo.getPhoneNum());
        //System.out.println(authorities);
        model.addAttribute("role",authorities);
        return "/pages/all-admin-profile";
    }
    //修改个人信息
    @PostMapping("/changeProfile")
    public String changeProfile( HttpSession session,
                                 @RequestParam(name = "email") String email,
                                 @RequestParam(name = "phoneNum") String phoneNum,
                                 @RequestParam(name = "status") int status) throws Exception {
        SecurityContextImpl securityContext = (SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT");
        //取出登录用户
        String uname = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getUsername();
        UserInfo userkeys = userMapper.findByUsername(uname);
        UserInfo user = userInfoService.findById(userkeys.getId());
        user.setEmail(email);
        user.setPhoneNum(phoneNum);
        user.setStatus(status);
        userInfoService.updateUserInfo(user);
        return "redirect:findProfile";
    }
    //修改密码
   // @Async
    @RequestMapping("/changePassword")
    public String changgePassword(HttpSession session,@RequestParam(name = "newpassword") String password) throws MessagingException {
        SecurityContextImpl securityContext = (SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT");
        //取出登录用户
        String uname = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getUsername();
        UserInfo user = userMapper.findByUsername(uname);
        user.setPassword(bCryptPasswordEncoder.encode(password));
        userInfoService.updateUserInfo(user);
        mailService.sendMail(user.getEmail());
        return "redirect:findProfile";
    }
    //密码验证
    @ResponseBody
    @RequestMapping("/checkPassword")
    public Msg checkPassword(@RequestParam(name = "oldpassword") String oldpassword,HttpSession session){
        SecurityContextImpl securityContext = (SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT");
        //取出登录用户
        String uname = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getUsername();
        UserInfo userInfo = userMapper.findByUsername(uname);
        boolean matches = bCryptPasswordEncoder.matches(oldpassword, userInfo.getPassword());
        if (matches){
            return Msg.success();
        }else {
            return Msg.error();
        }
    }
}

